from typing import List, Optional
from uuid import UUID
from datetime import datetime
from pydantic import BaseModel, Field


class DocumentComplianceRelationBase(BaseModel):
    """文档与检查项关联基础模型"""
    document_id: UUID = Field(..., description="文档ID")
    compliance_template_item_id: UUID = Field(..., description="合规检查项ID")
    relation_type: str = Field(default="reference", description="关联类型: reference(参考), evidence(证据), template(模板)")


class DocumentComplianceRelationCreate(DocumentComplianceRelationBase):
    """创建文档与检查项关联请求模型"""
    pass


class DocumentComplianceRelationResponse(DocumentComplianceRelationBase):
    """文档与检查项关联响应模型"""
    id: UUID
    created_at: datetime

    class Config:
        from_attributes = True


class DocumentComplianceRelationWithDetails(DocumentComplianceRelationResponse):
    """带详细信息的文档与检查项关联响应模型"""
    document_title: Optional[str] = Field(description="文档标题")
    document_filename: Optional[str] = Field(description="文档文件名")
    compliance_template_item_title: Optional[str] = Field(description="检查项标题")
    compliance_template_item_code: Optional[str] = Field(description="检查项编码")


class BatchRelationCreateRequest(BaseModel):
    """批量创建关联关系请求模型"""
    relations: List[DocumentComplianceRelationCreate] = Field(..., description="关联关系列表", min_items=1)


class RelationTypeStats(BaseModel):
    """关联类型统计"""
    relation_type: str = Field(description="关联类型")
    count: int = Field(description="数量")


class DocumentRelationSummary(BaseModel):
    """文档关联摘要"""
    document_id: UUID
    total_relations: int = Field(description="总关联数")
    by_type: List[RelationTypeStats] = Field(description="按类型统计")


class ComplianceTemplateItemRelationSummary(BaseModel):
    """检查项关联摘要"""
    compliance_template_item_id: UUID
    total_relations: int = Field(description="总关联数")
    by_type: List[RelationTypeStats] = Field(description="按类型统计")
